package studio.raptor.extractor.segment.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import studio.raptor.extractor.common.util.JdbcUtils;
import studio.raptor.extractor.segment.config.EventSourceConfig;

/**
 * Created by liuyong8 on 2017/8/1.
 */
@Repository
public class EventSourceConfigDao {
    private static final Logger log = LoggerFactory.getLogger(EventSourceConfigDao.class);

    @Autowired
    @Qualifier("extractorDataSource")
    private DataSource dataSource;

    private static final String QUERY_CONFIGS = "SELECT t.event_kind_id,t.event_name,t.event_source,t.event_sort,t.databus_kind_id FROM databus.event_source_config t";
    private static final String QUERY_CONFIG = "SELECT t.event_kind_id,t.event_name,t.event_source,t.event_sort,t.databus_kind_id FROM databus.event_source_config t WHERE t.event_kind_id = ?";

    public EventSourceConfig queryEventSourceConfigByKindId(Integer kindId) throws Exception{
        Connection connection = null;
        PreparedStatement pstmt = null;
        EventSourceConfig eventSourceConfig = new EventSourceConfig();
        String sql = QUERY_CONFIG;
        try {
            connection = dataSource.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1,kindId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                eventSourceConfig.setEventKindId(rs.getInt("event_kind_id"));
                eventSourceConfig.setEventName(rs.getString("event_name"));
                eventSourceConfig.setEventSource(rs.getString("event_source"));
                eventSourceConfig.setEventSort(rs.getInt("event_sort"));
                eventSourceConfig.setDatabusKindId(rs.getInt("databus_kind_id"));
            }
        } catch (Exception e) {
            log.error("query event_source_config failed!", e);
            throw e;
        } finally {
            JdbcUtils.close(pstmt);
            JdbcUtils.close(connection);
        }
        return eventSourceConfig;
    }

    public List<EventSourceConfig> queryEventSourceConfigs() throws Exception{
        Connection connection = null;
        PreparedStatement pstmt = null;
        List<EventSourceConfig> configs = new ArrayList<>();
        String sql = QUERY_CONFIGS;
        try {
            connection = dataSource.getConnection();
            pstmt = connection.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                EventSourceConfig eventSourceConfig = new EventSourceConfig();
                eventSourceConfig.setEventKindId(rs.getInt("event_kind_id"));
                eventSourceConfig.setEventName(rs.getString("event_name"));
                eventSourceConfig.setEventSource(rs.getString("event_source"));
                configs.add(eventSourceConfig);
            }
        } catch (Exception e) {
            log.error("query event_source_config failed!", e);
            throw e;
        } finally {
            JdbcUtils.close(pstmt);
            JdbcUtils.close(connection);
        }
        return configs;
    }
}
